﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace vmMachine
{
    //VendingMaching is a concrete Observer
    class VendingMachine : Subject
    {
        private double moneyAmount;
        private String machineDisplay;
        private double givenChange;
        protected List<Item> items;
        private List<Observer> observers;


        public void notify(int i)
        {
            //foreach observer => observer.update
            foreach (Observer o in observers)
            {
                o.update(i);
            }
        }

        public void attach(Observer o)
        {
            observers.Add(o);
        }

        public void detach(Observer o)
        {
            Observer temp=null;
            foreach (Observer form in observers)
            {
                if (form.Equals(o))
                {
                    temp = o;
                }
            }
            try
            {
                observers.Remove(temp);
            }
            catch { };

        }

        public VendingMachine(Form1 form)
        {
            this.moneyAmount = 0;
            machineDisplay = "00.00";
            givenChange = 0;
            items = new List<Item>();
            
            //adding items to the item list
            Item coca_cola = new Item(7, 1);
            items.Add(coca_cola);
            Item water = new Item(5, 10);
            items.Add(water);
            Item prigat = new Item(7, 10);
            items.Add(prigat);
            Item pepsi = new Item(7, 10);
            items.Add(pepsi);
            Item paz = new Item(5.5, 10);
            items.Add(paz);
            Item fanta = new Item(7, 10);
            items.Add(fanta);

            this.observers = new List<Observer>();
            attach((Observer)form);
        }


        public double MoneyAmount
        {
            set
            {
                if (value < 0)
                {
                    this.moneyAmount = 0;
                }
                else
                {
                    this.moneyAmount = value;
                }
            }

            get
            {
                return this.moneyAmount;
            }
        }

        public double GivenChange
        {
            get
            {
                return this.givenChange;
            }
            set
            {
                this.givenChange = value;
            }
        }

        public String MachineDisplay
        {
            set
            {
                this.machineDisplay = value;
            }
            get
            {
                return this.machineDisplay;
            }
        }
    }
}

